<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <button id="btn">获取当前位置</button>

    <p id="address"></p>
<script>
    let btn = document.getElementById("btn");
    let address = document.getElementById("address");
    btn.onclick=function(){

        // navigator.geolocation.getCurrentPosition方法，用于获取当前位置。三个参数，第一个参数是获取成功时的回调函数，第二个参数是获取失败时的回调函数，第三个参数是配置对象。
        navigator.geolocation.getCurrentPosition(function(pos){
            // console.log(coor);
            // address.textContent = JSON.stringify(coor);
            address.textContent = pos.coords.latitude+","+pos.coords.longitude;
        },function(err){
            // console.log("获取失败");
            // console.log(err);
            alert("获取失败");
        },{
            timeout:10000
        })
    }
</script>
</body>
</html>